package com.borrowed.book.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.borrowed.book.book.constant.BookConstant;
import com.borrowed.book.book.domain.CommonIds;
import com.borrowed.book.book.entity.domain.SExpectBook;
import com.borrowed.book.book.entity.domain.SExpectBookImg;
import com.borrowed.book.book.entity.domain.vo.ExpectBookSelectVo;
import com.borrowed.book.book.entity.domain.vo.ExpectBookVo;
import com.borrowed.book.book.service.SExpectBookImgService;
import com.borrowed.book.book.service.SExpectBookService;
import com.borrowed.book.book.mapper.SExpectBookMapper;
import com.borrowed.book.system.domain.BaseSelectPagePojo;
import com.borrowed.book.system.domain.SysDictPojo;
import com.borrowed.book.system.service.ISysDictService;
import com.borrowed.book.user.domain.SUser;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
*
*/
@Service
public class SExpectBookServiceImpl extends ServiceImpl<SExpectBookMapper, SExpectBook> implements SExpectBookService{
    @Autowired
    private ISysDictService iSysDictService;
    @Autowired
    private SExpectBookImgService expectBookImgService;

    /**
     * 添加期待书单
     * @param expectBookVo
     */
    @Override
    public void AddExpectBook(ExpectBookVo expectBookVo) {
        //期待图书保存
        SExpectBook expectBook=new SExpectBook();
        expectBookVo.setBookStatus(BookConstant.expect_book_create);
        this.compeleData(expectBook,expectBookVo);
        boolean flag=this.save(expectBook);
        if(expectBookVo.getBookImgs()!=null){
            for(SExpectBookImg img:expectBookVo.getBookImgs()){
                img.setCreateTime(new Date());
                img.setExpectBookId(expectBook.getId());
                expectBookImgService.save(img);
            }
        }
    }

    /**
     * 更新期待图书
     * @param expectBookVo
     */
    @Override
    public void UpdateExpectBook(ExpectBookVo expectBookVo) {
        //期待图书更新
        SExpectBook expectBook=new SExpectBook();
        this.compeleData(expectBook,expectBookVo);
        this.updateById(expectBook);
        //删除关联的图片，重新保存
        QueryWrapper<SExpectBookImg> wrapper=new QueryWrapper<>();
        wrapper.eq("expect_book_id",expectBook.getId());
        expectBookImgService.remove(wrapper);
        if(expectBookVo.getBookImgs()!=null){
            for(SExpectBookImg img:expectBookVo.getBookImgs()){
                img.setCreateTime(new Date());
                img.setExpectBookId(expectBook.getId());
                expectBookImgService.save(img);
            }
        }
    }

    /**
     * 批量删除图书
     * @param param
     */
    @Override
    public void DeleteExpectBook(CommonIds param) {
        if(param.getIds()!=null&&param.getIds().size()>0) {
            for(Long id:param.getIds()) {
                QueryWrapper<SExpectBookImg> wrapper=new QueryWrapper<>();
                wrapper.eq("expect_book_id",id);
                expectBookImgService.remove(wrapper);
            }
            this.removeByIds(param.getIds());
        }
    }

    /**
     * 期待书单一览查询
     * @return
     */
    @Override
    public List<ExpectBookVo> SelectExpectBook() {
        SUser sysUser = (SUser) SecurityUtils.getSubject().getPrincipal();

        List<ExpectBookVo> data=new ArrayList<>();

        QueryWrapper<SExpectBook> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("open_id",sysUser.getOpenid());
        queryWrapper.orderByDesc("create_time");
        List<SExpectBook> list=this.list(queryWrapper);
        for(SExpectBook expectBook:list){
            ExpectBookVo bookVo=new ExpectBookVo();
            this.compeleVoData(expectBook,bookVo);
            QueryWrapper<SExpectBookImg> wrapper=new QueryWrapper<>();
            wrapper.eq("expect_book_id",expectBook.getId());
            List<SExpectBookImg> bookImgs=expectBookImgService.list(wrapper);
            bookVo.setBookImgs(bookImgs);
            data.add(bookVo);
        }
        return data;
    }

    /**
     * 管理员一览查询
     * @return
     */
    @Override
    public List<ExpectBookVo> SelectExpectBookAdmin(ExpectBookSelectVo param) {
        List<ExpectBookVo> data=new ArrayList<>();

        QueryWrapper<SExpectBook> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByAsc("book_name");
        queryWrapper.orderByDesc("create_time");
        if(param.getBookStatus()!=null){
            queryWrapper.eq("book_status",param.getBookStatus());
        }
        List<SExpectBook> list=this.list(queryWrapper);
        for(SExpectBook expectBook:list){
            ExpectBookVo bookVo=new ExpectBookVo();
            this.compeleVoData(expectBook,bookVo);
            QueryWrapper<SExpectBookImg> wrapper=new QueryWrapper<>();
            wrapper.eq("expect_book_id",expectBook.getId());
            List<SExpectBookImg> bookImgs=expectBookImgService.list(wrapper);
            bookVo.setBookImgs(bookImgs);
            data.add(bookVo);
        }
        return data;
    }


    private void compeleData(SExpectBook  expectBook,ExpectBookVo bookVo){
        SUser sysUser = (SUser) SecurityUtils.getSubject().getPrincipal();

        expectBook.setAuthName(bookVo.getAuthName());
        expectBook.setBookName(bookVo.getBookName());
        expectBook.setBookSeriesName(bookVo.getBookSeriesName());
        expectBook.setBookStatus(bookVo.getBookStatus());
        expectBook.setPress(bookVo.getPress());
        expectBook.setOpenId(sysUser.getOpenid());
        if(bookVo.getId()!=null){
            expectBook.setId(bookVo.getId());
            expectBook.setUpdateTime(new Date());
        }else{
            expectBook.setCreateTime(new Date());
        }
    }

    private void compeleVoData(SExpectBook  expectBook,ExpectBookVo bookVo){
        bookVo.setAuthName(expectBook.getAuthName());
        bookVo.setBookName(expectBook.getBookName());
        bookVo.setBookSeriesName(expectBook.getBookSeriesName());
        bookVo.setBookStatus(expectBook.getBookStatus());
        bookVo.setPress(expectBook.getPress());
        bookVo.setId(expectBook.getId());
        bookVo.setCreateTime(expectBook.getCreateTime());
    }


}
